package com.best.oasis.settlement.util;

import java.util.Timer;
import java.util.TimerTask;

import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.apache.log4j.Logger;

public class Config extends TimerTask {
	
	 private static final Logger logger = Logger.getLogger(Config.class);
	 private static PropertiesConfiguration properties = null;
	 private static Timer timer;
	    
	 static {
	        load();
	    }

	    public static void schedule() {
	        timer = new Timer("TimerTask-config", false);
	        timer.scheduleAtFixedRate(new Config(), 2000, Config.getConfigReloadInterval());
	    }

	    public void run() {
	        long lastConfigReloadInterval = Config.getConfigReloadInterval();
	        boolean loadResult = load();
	        if(!loadResult){
	            timer.cancel();
	            return;
	        }
	        long newConfigReloadInterval = Config.getConfigReloadInterval();
	        
	        if(lastConfigReloadInterval != newConfigReloadInterval){
	            timer.cancel();
	            schedule();
	        }
	    }
	    public static boolean load() {
	        try {
	            properties = new PropertiesConfiguration();
	            java.net.URL url = Thread.currentThread().getContextClassLoader().getResource("systemConfig.properties");
	            if(url != null){
	                properties.load(url);
	                return false;
	            }
	            properties.setReloadingStrategy(new FileChangedReloadingStrategy());
	            properties.setEncoding("UTF-8");
	            properties.setAutoSave(false);
	        } catch (Exception ex) {
	            ex.printStackTrace();
	            return false;
	        }
	        return true;

	    }

	    private static int getConfigReloadInterval() {
	        int result = 10000;
	        try{
	            result = getProperties().getInt("config.reloadInteval");
	            //result = Integer.parseInt(resultStr);
	        }catch(Exception e){
	        }
	        return result;
	    }
	    public static PropertiesConfiguration getProperties() {
	        return properties;
	    }
	    public static String getSearchFilter() {
	        return getProperties().getString("ldap.FilterBasedLdapUserSearch.searchFilter");
	    } 
	    /**
	     * 重置的默认密码
	     */
	    public static String getUserDefaultResetPassword() {
	        return getProperties().getString("user.defaultResetPassword");
	    } 
	    
	    /**
	     * 汇通AD验证信息
	     * @return
	     */
	    public static String getHtLdapUrl() {
	        return getProperties().getProperty("ldap.DefaultSpringSecurityContextSource.ht.url").toString();
	    } 
	    public static String getHtLdapUserDn() {
	        return getProperties().getString("ldap.DefaultSpringSecurityContextSource.ht.userDn");
	    } 
	    public static String getHtLdapPassword() {
	        return getProperties().getString("ldap.DefaultSpringSecurityContextSource.ht.password");
	    } 
	    /**
	     * 百世AD验证信息
	     * @return
	     */
	    public static String getBestLdapUrl() {
	        return getProperties().getString("ldap.DefaultSpringSecurityContextSource.best.url_1") + " " 
	             + getProperties().getString("ldap.DefaultSpringSecurityContextSource.best.url_2") ;
	    } 
	    public static String getBestLdapUserDn() {
	        return getProperties().getString("ldap.DefaultSpringSecurityContextSource.best.userDn");
	    } 
	    public static String getBestLdapPassword() {
	        return getProperties().getString("ldap.DefaultSpringSecurityContextSource.best.password");
	    } 
	    public static void main(String args[]) {
	        try {
	            //int tmp = Config.getProperties().getProperty("config.reloadInteval");
	            //System.out.println("config.reloadInteval=" + tmp);
	
	            logger.info("ldap.FilterBasedLdapUserSearch.searchFilter=" + getSearchFilter());
	            //汇通AD验证信息
	            logger.info("ldap.DefaultSpringSecurityContextSource.ht.url=" + getHtLdapUrl());
	            logger.info("ldap.DefaultSpringSecurityContextSource.ht.userDn=" + getHtLdapUserDn());
	            logger.info("ldap.DefaultSpringSecurityContextSource.ht.password=" + getHtLdapPassword());
	            
	            //百世AD验证信息
	            logger.info("ldap.DefaultSpringSecurityContextSource.best.url=" + getBestLdapUrl());
	            logger.info("ldap.DefaultSpringSecurityContextSource.best.userDn=" + getBestLdapUserDn());
	            logger.info("ldap.DefaultSpringSecurityContextSource.best.password=" + getBestLdapPassword());
	            
	            
	        } catch (Exception ex) {
	        	ex.printStackTrace();
	        	 System.out.println("config.reloadInteval=" + ex.getMessage());
	        	 if (logger.isDebugEnabled()) {
	                 logger.debug(ex);
	             }
	        }
	    }
}
